package cn.ubeta.b2c.user.address;

import android.app.Activity;
import android.graphics.Typeface;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.widget.TextView;

import cn.ubeta.b2c.R;
import cn.ubeta.b2c.api.ApiUser;
import cn.ubeta.b2c.bean.AddressInfo;
import cn.ubeta.b2c.common.ListFragment;
import cn.ubeta.b2c.common.ListFragmentConfig;
import cn.ubeta.b2c.utils.AppConfig;
import cn.ubeta.b2c.utils.ToastHelper;
import cn.ubeta.b2c.widget.recyclerview.BaseViewHolder;

/**
 * Created by denny on 16/9/6.
 */

public class AddressFragment extends ListFragment<AddressInfo> {

    public static Fragment newInstance() {
        AddressFragment fragment = new AddressFragment();
        return fragment;
    }

    @Override
    protected ListFragmentConfig getConfig() {
        return new ListFragmentConfig(R.layout.item_address);
    }

    @Override
    protected void bindItemView(BaseViewHolder helper, AddressInfo item) {
        // 名字
        final String name = item.getDisplayName();
        if (!TextUtils.isEmpty(name)) {
            ((TextView) helper.getView(R.id.tv_name)).setText(name);
        }

        // 详情
        TextView tvDetail = helper.getView(R.id.tv_address_detail);
        final String detail = item.isAddressDefault() ? (item.getAddressDetail() + "[默认]") : item.getDisplayAddressDetail();
        if (!TextUtils.isEmpty(detail)) {
            tvDetail.setText(detail);
        }
        tvDetail.setTypeface(null, item.isAddressDefault() ? Typeface.BOLD : Typeface.NORMAL);

        //helper.getConvertView().setOnClickListener(v -> EditAddressActivity.gotoAddressEdit(getActivity(), item.getAddressId()));
        helper.getConvertView().setOnClickListener(v -> {
            AppConfig.savePreConfigAddress(item);
            getActivity().setResult(Activity.RESULT_OK);
            getActivity().finish();
        });
    }

    @Override
    protected void loadData(int page) {
        ApiUser.getAddressList(page, getCallback());
    }

    @Override
    protected void onLoadFinish(boolean respOk) {
        super.onLoadFinish(respOk);
        if (respOk && (mAdapter == null || mAdapter.getItemCount() == 0)) {
            ToastHelper.show("请到京东主站添加收货地址");
        }
    }
}
